/*******************************************************************************
 * Package: com.song.boot.springstudy.webflux
 * Type:    MapMain
 * Date:    2025-04-06 16:36
 *
 * Copyright (c) 2025 LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.song.boot.springstudy.webflux;

import lombok.SneakyThrows;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * 功能描述：
 *
 * @author Songxianyang
 * @date 2025-04-06 16:36
 */
public class MapMain {
    @SneakyThrows
    public static void main(String[] args) {
        Flux.just(1,2,3).map(v->v*2).subscribe(
                s-> System.out.println(s),
                System.err::println,
                ()-> System.out.println("处理完成")
        );
        System.out.println("----------");
        Flux.just("song","xian","yang").map(v-> "hello"+v)
                // 加索引
                .index()
                .subscribe(
                        // 二元组
                        s-> System.out.println(s.getT1()+"-"+s.getT2()),
                        System.err::println,
                        ()-> System.out.println("处理完成")
                );

        System.out.println("----------");
        Flux.just("song","xian","yang").map(v-> "hello"+v)
                // 时间戳
                .timestamp()
                .subscribe(
                        // 二元组
                        s-> System.out.println(s.getT1()+"-"+s.getT2()),
                        System.err::println,
                        ()-> System.out.println("处理完成")
                );


        // flatMap并发执行 输出顺序不一致
        Flux.just("song xian","li si","wang wu")
                .flatMap(s -> Flux.just(s.split(" "))
                        .doOnSubscribe(subscription -> {
                            System.out.println("订阅");
                        })
                )
                .subscribe(System.out::println);
        // 按顺序执行  性能差
        Flux.just("song xian","li si","wang wu")
                .concatMap(s -> Flux.just(s.split(" "))
                        .doOnSubscribe(subscription -> {
                            System.out.println("订阅concatMap");
                        })
                )
                .subscribe(System.out::println);

        // Thread.sleep(10000);
    }
}
